package io.gitee.zkool.barcodegun.pc.ws;

import org.eclipse.jetty.server.*;
import org.eclipse.jetty.servlet.DefaultServlet;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.servlet.ServletHolder;
import org.eclipse.jetty.util.resource.Resource;
import org.eclipse.jetty.util.ssl.SslContextFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;

/**
 * @author zhaokk
 * @since 2025/1/21
 */
public class EmbeddedJettyWithP12 {

    private static Logger log = LoggerFactory.getLogger(EmbeddedJettyWithP12.class);
    // 默认端口
    public static final int HTTPS_PORT = 2025;

    public void server() throws URISyntaxException, MalformedURLException {
        Server server = new Server();

        // HTTP配置
        HttpConfiguration httpConfig = new HttpConfiguration();
        httpConfig.setSecureScheme("https");
        httpConfig.setSecurePort(HTTPS_PORT);
        httpConfig.setOutputBufferSize(32768);
        // 将自定义请求自定义器添加到HTTP配置
        httpConfig.addCustomizer(new SecureRequestCustomizer());

        // 配置SSL
//        SslContextFactory sslContextFactory = new SslContextFactory();
        SslContextFactory.Server sslContextFactory = new SslContextFactory.Server();
        // 证书路径
        URI keyStoreUri = EmbeddedJettyWithP12.class.getClassLoader().getResource("keystore.p12").toURI();
        sslContextFactory.setKeyStoreResource(Resource.newResource(keyStoreUri));
        sslContextFactory.setKeyStorePassword("123456"); // 证书密码

        // 创建HTTP连接器
/*        ServerConnector httpConnector = new ServerConnector(server, new HttpConnectionFactory(httpConfig));
        httpConnector.setPort(8080);
        server.addConnector(httpConnector);*/

        // 创建HTTPS连接器
        ServerConnector httpsConnector = new ServerConnector(server,
                new SslConnectionFactory(sslContextFactory, "http/1.1"),
                new HttpConnectionFactory(httpConfig));
        httpsConnector.setPort(HTTPS_PORT);
        server.addConnector(httpsConnector);

        ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
        context.setContextPath("/");

        // 添加一个简单的 Servlet
        context.addServlet(new ServletHolder(new HelloWorldServlet()), "/test/*");

        // 将静态文件夹（例如 "static"）设置为资源目录
        // 假设你的JavaScript文件位于src/main/resources/static/js
        URI resourceUri = EmbeddedJettyWithP12.class.getClassLoader().getResource("static").toURI();
        context.setBaseResource(Resource.newResource(resourceUri));

        // 添加一个资源处理程序来服务静态内容
        context.addServlet(new ServletHolder(new DefaultServlet()), "/*");

        server.setHandler(context);

        try {
            server.start();
            server.join();
        } catch (Exception e) {
            log.error("Failed to start server", e);
        }
    }

    // 一个简单的 Servlet 示例
    public class HelloWorldServlet extends javax.servlet.http.HttpServlet {
        @Override
        protected void doGet(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, java.io.IOException {
            response.setContentType("text/html");
            response.setStatus(javax.servlet.http.HttpServletResponse.SC_OK);
            response.getWriter().println("<h1>Hello World</h1>");
        }
    }

}
